package com.zhang.backend.web.controller;

import com.zhang.backend.common.utls.SessionUtils;
import com.zhang.backend.model.vo.UserVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhang.backend.common.JsonResponse;
import com.zhang.backend.service.UserService;
import com.zhang.backend.model.domain.User;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Objects;
import java.util.Random;


/**
 * 前端控制器
 *
 * @author zyx
 * @version v1.0
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * 描述：根据Id 查询
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id) throws Exception {
        User user = userService.getById(id);
        return JsonResponse.success(user);
    }

    /**
     * 描述：根据Id删除
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        userService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
     * 描述：根据Id 更新
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateUser(@PathVariable("id") Long id, User user) throws Exception {
        user.setId(id);
        userService.updateById(user);
        return JsonResponse.success(null);
    }


    /**
     * 描述:创建User
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(User user) throws Exception {
        userService.save(user);
        return JsonResponse.success(null);
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    @ResponseBody
    public JsonResponse login(@RequestBody UserVO userVO) {
        User loginUser = userService.login(userVO);
        if (Objects.nonNull(loginUser)) {
            SessionUtils.saveCurrentUserInfo(loginUser);
        }
        return JsonResponse.success(loginUser);
    }

    /**
     * 游客登录
     */
    @GetMapping("/login")
    @ResponseBody
    public JsonResponse nologin() {
        SessionUtils.saveCurrentUserInfo(new User().setRoleId("2").setName("游客账号"+ new Random().nextInt(100)));
        return JsonResponse.success(null);
    }



    /**
     * 等出
     */
    @GetMapping("/logout")
    public String logout() {
        SessionUtils.clear();
        return "login";
    }

    /**
     * 下载
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response, @RequestBody User user) {
        //1.不需要返回
        //2.参数要带有响应
        userService.export(response, user);
    }

    @PostMapping("/import")
    @ResponseBody
    public JsonResponse importUser(MultipartFile file) {
        Map<String, Object> map = userService.importUser(file);
        return JsonResponse.success(map);
    }

    @GetMapping("/staByCollege")
    @ResponseBody
    public JsonResponse staByCollege() {
        return JsonResponse.success(userService.staByCollege());
    }
}


